// licenseTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <NCore.h>
#include <NImage.h>
#include <SentiSight.h>
#include <NLicensing.h>

#include "apfastTiming.h"

int _tmain(int argc, _TCHAR* argv[])
{
	NChar *components = N_T("SentiSight");
	NBool available;
	NResult result;
	HSEEngine engine;
	HSEModel model;
	HSEShape shape;
	NPointD point[3];
	NUInt refId;
	SEStatus status;
	NBool isRecognized;
	bool ret;
	apHiResElapsedTime tick;

	printf("before verify license...\n");
	result = NCoreOnStart();
	if (NFailed(result))
	{
		printf("SE engine: Failed to start\n");
		return -1;
	}

	//"/local"
	result = NLicenseObtainComponents(N_T("/local"), N_T("5000"), components, &available);
	if (NFailed(result))
	{
		printf("NLicenseObtainComponents() failed, result = %d\n", result);
		return -1;
	}
	if(available != NTrue)
	{
		printf("lisence %s not available\n", components);
	}

	// A m_hSEEngine handle must be allocated. This handle will be used in the whole object learning process
	printf("before SECreate\n");
	result = SECreate(&engine);
	if(NFailed(result))
	{
		if(result == N_E_NOT_SUPPORTED)
		{
			printf("your processor does not support necessary SSE instructions");
		}
		else
		{
			printf("failed to initialize SentiSight (result = %d)\n", result);
		}
		getchar();
		return -1;
	}

	HNImage hImage = NULL;

	// Create image from file
	result = NImageCreateFromFileEx((NChar *)".\\11.bmp", NULL, 0, NULL, &hImage);
	if(NFailed(result))
	{
		printf("failed to open image (result = %d)\n", result);
		return -1;
	}

	result = SECreateModelEx(engine, 0, &model);
	result = SEShapeCreate(&shape);
	point[0].X = 0.0f;
	point[0].Y = 0.0f;
	point[1].X = 10.0f;
	point[1].Y = 0.0f;
	point[2].X = 10.0f;
	point[2].Y = 10.0f;
	SEShapeAddPointEx(shape, point);

	// Add image to model
	printf("before SELrnAddToModelEx...\n");
	tick.reset();
	//while(1)
	{
		result = SELrnAddToModelEx(engine, model, hImage, 0, NULL, 
			NULL, &refId, &status);
		if(NFailed(result))
		{
			printf("failed to learn model in frame (result = %d), sec = %.2f\n",
				result, tick.sec());
			getchar();
			return -1;
			//break;
		}
	}


	printf("before recognize...\n");
	tick.reset();

	while(1)
	{
		result = SERecRecognizeImageEx(engine, hImage, &isRecognized);
		if(NFailed(result))
		{
			printf("failed to recognize model in frame (result = %d), sec %.2f sec\n",
				result, tick.sec());
			break;
		}

		if (isRecognized == NTrue)
		{
			printf("recognized\n");
		}
	}


	getchar();
	return 0;
}

